package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.constant.ProductConst;
import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.BaseAttrInfo;
import com.atguigu.gmall.model.product.BaseTrademark;
import com.atguigu.gmall.model.product.SkuInfo;
import com.atguigu.gmall.model.product.SpuInfo;
import com.atguigu.gmall.product.service.ManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * projectName: gmall
 *
 * @author: 陈鹏
 * time: 2022/9/21 16:17 星期三
 * description:后台管理使用的api的控制层
 */
@RestController
@RequestMapping(value = "/admin/product")
public class ManagerController {

    @Autowired
    private ManagerService managerService;

    /**
     * 查询所有的一级分类
     * @return
     */
    @GetMapping(value = "/getCategory1")
    public Result getCategory1(){
        return Result.ok(managerService.getBaseCategory1());
    }

    /**
     * 根据一级分类查询二级分类
     * @param c1Id
     * @return
     */
    @GetMapping(value = "/getCategory2/{c1Id}")
    public Result getCategory2(@PathVariable("c1Id") Long c1Id){
        return Result.ok(managerService.getBaseCategory2(c1Id));
    }

    /**
     * 根据二级分类查询三级分类
     * @param c2Id
     * @return
     */
    @GetMapping(value = "/getCategory3/{c2Id}")
    public Result getCategory3(@PathVariable("c2Id") Long c2Id){
        return Result.ok(managerService.getBaseCategory3(c2Id));
    }

    /**
     * 保存平台属性
     * @param baseAttrInfo
     * @return
     */
    @PostMapping(value = "/saveAttrInfo")
    public Result saveAttrInfo(@RequestBody BaseAttrInfo baseAttrInfo){
        managerService.saveBaseAttrInfo(baseAttrInfo);
        return Result.ok();
    }

    /**
     * 查询平台属性列表
     * @param category1Id
     * @param category2Id
     * @param category3Id
     * @return
     */
    @GetMapping(value = "/attrInfoList/{category1Id}/{category2Id}/{category3Id}")
    public Result attrInfoList(@PathVariable("category1Id") Long category1Id,
                               @PathVariable("category2Id") Long category2Id,
                               @PathVariable("category3Id") Long category3Id){
        return Result.ok(managerService.getBaseAttrInfo(category1Id,category2Id,category3Id));
    }

    /**
     * 查询平台值列表
     * @param attrId
     * @return
     */
    @GetMapping(value = "/getAttrValueList/{attrId}")
    public Result getAttrValueList(@PathVariable(value = "attrId") Long attrId){
        return Result.ok(managerService.getBaseAttrValue(attrId));
    }

    /**
     * 查询品牌列表
     * @return
     */
    @GetMapping(value = "/baseTrademark/getTrademarkList")
    public Result getTrademarkList(){
        return Result.ok(managerService.getBaseTrademark());
    }

    /**
     * 查询所有销售属性
     * @return
     */
    @GetMapping(value = "/baseSaleAttrList")
    public Result getSaleAttrList(){
        return Result.ok(managerService.getBaseSaleAttr());
    }

    /**
     * 保存spuInfo信息
     * @param spuInfo
     * @return
     */
    @PostMapping(value = "/saveSpuInfo")
    public Result saveSpuInfo(@RequestBody SpuInfo spuInfo){
        managerService.saveSpuInfo(spuInfo);
        return Result.ok();
    }

    /**
     * 分页查询spuInfo信息
     * @param page
     * @param size
     * @param category3Id
     * @return
     */
    @GetMapping(value = "/{page}/{size}")
    public Result getSpuInfo(@PathVariable("page") Integer page,
                             @PathVariable("size") Integer size,
                             Long category3Id){
        return Result.ok(managerService.getSpuInfoList(page,size,category3Id));
    }

    /**
     * 查询所有的品牌
     * @param page
     * @param size
     * @return
     */
    @GetMapping(value = "/baseTrademark/{page}/{size}")
    public Result baseTrademark(@PathVariable("page") Integer page,
                                @PathVariable("size") Integer size){
        return Result.ok(managerService.getBaseTrademarkList(page,size));
    }

    /**
     * 根据Id回显品牌
     * @param id
     * @return
     */
    @GetMapping(value = "/baseTrademark/get/{id}")
    public Result searchBaseTrademarkById(@PathVariable(value = "id") Long id){
        return Result.ok(managerService.getBaseTrademarkById(id));
    }

    /**
     * 根据Id删除品牌
     * @param id
     * @return
     */
    @DeleteMapping(value = "/baseTrademark/remove/{id}")
    public Result deleteBaseTrademarkById(@PathVariable(value = "id") Long id){
        managerService.deleteBaseTrademarkById(id);
        return Result.ok();
    }

    /**
     * 修改品牌
     * @param baseTrademark
     * @return
     */
    @PutMapping(value = "/baseTrademark/update")
    public Result updateBaseTrademark(@RequestBody BaseTrademark baseTrademark){
        managerService.updateBaseTrademark(baseTrademark);
        return Result.ok();
    }

    /**
     * 新增品牌
     * @param baseTrademark
     * @return
     */
    @PostMapping(value = "baseTrademark/save")
    public Result saveBaseTrademark(@RequestBody BaseTrademark baseTrademark){
        managerService.saveBaseTrademark(baseTrademark);
        return Result.ok();
    }

    /**
     * 分页查询skuInfo列表
     * @param page
     * @param size
     * @return
     */
    @GetMapping(value = "list/{page}/{size}")
    public Result getSkuInfoList(@PathVariable(value = "page") Integer page,
                                 @PathVariable(value = "size") Integer size){
        return Result.ok(managerService.skuInfoList(page,size));
    }

    /**
     * 根据spuId查询所有的销售信息
     * @param spuId
     * @return
     */
    @GetMapping(value = "/spuSaleAttrList/{spuId}")
    public Result spuSaleAttrList(@PathVariable("spuId") Long spuId){
        return Result.ok(managerService.getSpuSaleAttr(spuId));
    }

    /**
     * 根据spuId查询图片
     * @param spuId
     * @return
     */
    @GetMapping(value = "/spuImageList/{spuId}")
    public Result spuImageList(@PathVariable(value = "spuId") Long spuId){
        return Result.ok(managerService.getSpuImage(spuId));
    }

    /**
     * 保存sku信息
     * @param skuInfo
     * @return
     */
    @PostMapping(value = "/saveSkuInfo")
    public Result saveSkuInfo(@RequestBody SkuInfo skuInfo){
        managerService.saveSkuInfo(skuInfo);
        return Result.ok();
    }

    /**
     * 上架
     * @param skuId
     * @return
     */
    @GetMapping(value = "/onSale/{skuId}")
    public Result onSale(@PathVariable(value = "skuId") Long skuId){
        managerService.upOrDown(skuId, ProductConst.SKU_ON_SALE);
        return Result.ok();
    }

    /**
     * 下架
     * @param skuId
     * @return
     */
    @GetMapping(value = "/cancelSale/{skuId}")
    public Result cancelSale(@PathVariable(value = "skuId") Long skuId){
        managerService.upOrDown(skuId,ProductConst.SKU_CANCEL_SALE);
        return Result.ok();
    }
}
